﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace X9Tech.HashWall.Core.Stream
{
    public class WallStream
    {
        Twitter.SearchAPI.SearchContext sC = null;
        private string hashTag = string.Empty;
        public WallStream(string hashTagTerm)
        {
            hashTag = hashTagTerm;
            Twitter.SearchAPI.TwitterSearchQuery sq = new Twitter.SearchAPI.TwitterSearchQuery();
            sq.Hashtag = hashTagTerm;
            sC = new Twitter.SearchAPI.SearchContext(sq);
        }

        private int iSecondsRefreshInterval = 30;
        public int RefreshIntervalSeconds
        {
            get { return iSecondsRefreshInterval; }
            set { iSecondsRefreshInterval = value; }
        }

        public string HashTagSearchTerm { get { return hashTag; } }

        public List<Models.WallMessage> GetNewMessages()
        {
            List<Models.WallMessage> msgs = new List<Models.WallMessage>();
            List<Twitter.SearchAPI.TwitterSearchResult> results = sC.FetchNewResults();

            foreach (Twitter.SearchAPI.TwitterSearchResult r in results)
            {
                Models.WallMessage newMsg = new Models.WallMessage();

                newMsg.MessageDateTime = r.PublishDate.Value;
                newMsg.MessageId = r.Id;
                newMsg.Text = r.Text;
                newMsg.UserDisplayName = r.UserDisplayName;

                try
                {
                    newMsg.UserImage = r.Image;
                }
                catch (Exception) { }

                msgs.Insert(0, newMsg);
            }

            return msgs;
        }
    }
}
